  $('.select2').select2();
  $('#reservation').daterangepicker({
  	locale: {
  		format: 'YYYY-MM-DD'
  	}
  });
  layui.use('table', function() {
  	var table = layui.table;

  });
  var dataKind = 1;
  getData(1);

  function getData(curr) {
  	$.ajax({
  		type: "get",
  		url: "https://www.retux.cn/MgCouponList",
  		async: true,
  		data: {
  			row: 12,
  			page: curr
  		},
  		success: function(data) {
  			if(data[0].length == 0) {
  				layer.msg('暂无数据');
  			}
  			dataView(data[0], curr);
  			pageDivd(curr, data[1])
  		}
  	});
  }

  $('.discount-kind').change(function() {
  	dataKind = 2;
  	getSearchData(1);

  })

  function getSearchData(curr) {
  	$.ajax({
  		type: "get",
  		url: "https://www.retux.cn/MgCouponSearch/",
  		async: true,
  		data: {
  			row: 12,
  			page: curr,
  			Kind: $('.discount-kind').select2('val')
  		},
  		success: function(data) {
  			if(data[0].length == 0) {
  				layer.msg('暂无数据');
  			}
  			dataView(data[0], 1);
  			pageDivd(curr, data[1])
  		}
  	});
  }
  
  //添加优惠券
  $('.add_qu').click(function() {
  	var data_rang = $('#reservation').val(); //时间范围
  	var start = data_rang.split(' -')[0];
  	var end = data_rang.split('- ')[1];
  	var Num = Number($('.qu_num').val());
  	if($('.qu_name').val() == '') {
  		layer.msg('优惠券名称不能为空')
  	} else if($('.qu_num').val() == '') {
  		layer.msg('优惠券数量不能为空')

  	} else if($('.qu_price').val() == '') {
  		layer.msg('优惠券金额不能为空')

  	} else if($('.qu_price').val() % 10 != 0) {
  		layer.msg('优惠券金额必须是10的整数倍')

  	} else if(isNaN($('.qu_price').val())) {
  		layer.msg('优惠券金额必须是数字')

  	} else if($('.qu_kind').val() == '') {
  		layer.msg('满减条件不能为空')

  	} else if($('.use_kind').select2('val') == '') {
  		layer.msg('使用条件不能为空')

  	} else if(data_rang == '') {
  		layer.msg('有限时间不能为空')

  	} else {

  		$.ajax({
  			type: "get",
  			url: "https://www.retux.cn/MgNewCoupon",
  			async: true,
  			data: {
  				Name: $('.qu_name').val(),
  				Number: $('.qu_num').val(),
  				start: start,
  				end: end,
  				Money: $('.qu_price').val(),
  				Contidion: $('.qu_kind').val(),
  				Scene: $('.use_kind').select2('val')
  			},
  			success: function(data) {
  				if(data == 'success') {
  					layer.msg('新增成功', {
  						time: 500
  					}, function() {
  						location.reload();
  					})
  				} else if(data == 'error') {
  					layer.msg('新增失败，优惠券重名');
  				} else {
  					layer.msg('新增失败');
  				}
  			}
  		});
  	}
  })
  //优惠券列表渲染
  function dataView(data, curr) {
  	$('.table_data').html("");
  	for(var i = 0; i < data.length; i++) {
  		var tableData = `
	  							<tr>
	  							<td>${(curr-1)*12+i+1}</td>
      			 				<td>${data[i][0]}</td>
      			 				<td>${data[i][1]}</td>
      			 				<td>${data[i][2]}</td>
      			 				<td>${data[i][3]}</td>
      			 				<td index = "${data[i][4]}"><button class="change_num">查看</button></td>
      			 				
      			 				<td index = "${data[i][4]}">
      			 					<a class="more_num" href="#">增加数量</a>
      			 					<a class="qu_delect" href="#">删除</a>
      			 				</td>
      			 			</tr>
	  					`;
  		$('.table_data').append(tableData);

  	}
  	quOperate();
  }
  $('.qu_name').on('input propertychange', function() {
  	if($(this).val().length > 10) {
  		layer.msg('上面描述不能超过10个字符');
  		$(this).val('')
  	}
  });
   
  //优惠券列表操作
  function quOperate() {
  	var qu_prevId = '';
  	//查看兑换码
  	$('.change_num').click(function() {
  		var numId = $(this).parent().attr('index');
  		layer.open({
  			type: 2,
  			title: false,
  			closeBtn: 1,
  			area: ['750px', '500px'],
  			shade: 0.8,
  			shadeClose: true,
  			anim: 1,
  			id: 'LAY_layuipro',
  			btnAlign: 'c',
  			moveType: 1,
  			content: "http://119.23.212.200:30000/num_state.html?num=" + numId,
  			success: function() {

  			}

  		})

  	})
  	//关闭弹窗
  	$('.close_btn').click(function() {
  		$('.order_form').css('display', 'none');
  	})
  	//添加优惠券数量
  	$('.more_num').click(function() {
  		var Td = $(this).parents('tr').find('td');
  		qu_prevId = $(this).parent().attr('index');
  		$('.qu_name').val(Td.eq(1).text())
  		$('.date_pick').val(Td.eq(4).text())
  		//添加数量：除了数量可以输入其他都不可操作
  		$('.qu_name').attr('disabled', 'disabled');
  		$('.qu_price').attr('disabled', 'disabled');
  		$('.use_kind').attr('disabled', 'disabled');
  		$('.qu_kind').attr('disabled', 'disabled');
  		$('.date_pick').attr('disabled', 'disabled');
  		$('.add_qu').attr('disabled','disabled');
  		$('.add_qu').css('background','#C7C7C7');
  		$('.qu_num').val("");
  	})
  	$('.add_num').click(function() {
  		if(qu_prevId == '') {
  			layer.msg('请先选择一个优惠券再进行补充')
  		} else if($('.qu_num').val() == '') {
  			layer.msg('请补充优惠券数量再提交')

  		} else {
  			$.ajax({
  				type: "get",
  				url: "https://www.retux.cn/MgCouponControl",
  				async: true,
  				data: {
  					Id: qu_prevId,
  					Kind: 1,
  					Number: $('.qu_num').val()
  				},
  				success: function(data) {
  					if(data == 'success') {
  						layer.msg('新增成功', {
  							time: 500
  						}, function() {
  							location.reload();
  						})
  						qu_prevId = '';
  					} else {
  						layer.msg('操作失败')
  					}
  				}
  			});
  		}
  	})
  	//删除优惠券
  	$('.qu_delect').click(function() {
  		var quId = $(this).parent().attr('index');
  		$.ajax({
  			type: "get",
  			url: "https://www.retux.cn/MgCouponControl",
  			async: true,
  			data: {
  				Id: quId,
  				Kind: 2
  			},
  			success: function(data) {
  				if(data == 'success') {
  					layer.msg('删除成功', {
  						time: 500
  					}, function() {
  						location.reload();
  					})
  				} else {
  					layer.msg('操作失败')
  				}
  			}
  		});
  	})

  }

  function pageDivd(curr, page_num) {
  	layui.use(['laypage', 'layer'], function() {
  		var laypage = layui.laypage,
  			layer = layui.layer;
  		laypage.render({
  			elem: 'demo7',
  			count: page_num,
  			limit: 12,
  			curr: curr,
  			jump: function(obj, first) {
  				if(!first) {
  					if(dataKind == 1) {
  						getData(obj.curr);
  					} else {
  						getSearchData(obj.curr);
  					}

  				}
  			}
  		});
  	})
  }